package com.example.play.service.impl;


import com.example.play.entity.User;
import com.example.play.request.UserPageReq;
import com.example.play.request.UserReq;
import com.example.play.dao.UserMapper;
import com.example.play.service.UserService;
import cn.hutool.core.bean.BeanUtil;
import com.api.core.exception.ServiceException;
import com.api.core.result.PageView;
import com.api.core.util.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;




/**
 * 用户(User)表控制层
 *
 * @author makejava
 * @since 2022-02-26 11:32:20
 */
@Service("UserService")
public class UserServiceImpl extends PageUtils implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        userMapper.updateById(user);
        return userMapper.selectById(user.getId());
    }

    /**
     * 添加实现
     *
     * @param userReq !{tableInfo.comment}参数对象
     * @return User
     */
    @Override
    public User addUser(UserReq userReq) {
        Optional.ofNullable(userReq).orElseThrow(() -> new ServiceException("请求参数不能为空"));
        User user = new User();
        BeanUtil.copyProperties(userReq, user);
        user.setStatus(1);
        userMapper.insert(user);
        return user;
    }

    /**
     * 查询用户 列表
     *
     * @param userReq 编号
     * @return User
     */
    @Override
    public PageView findUserList(UserPageReq userReq) {
        Optional.ofNullable(userReq).orElseThrow(() -> new ServiceException("请求参数不能为空"));
        LambdaQueryWrapper<User> lambdaQuery = getUserQuery(userReq);
        Page<User> page = new Page<>(userReq.getNowPage(), userReq.getPageSize());
        Page userList = userMapper.selectPage(page, lambdaQuery);
        return getPageView(userList);
    }

    /**
     * 查询用户 详情
     *
     * @param userId user编号
     * @return User
     */
    @Override
    public User getUserById(Integer userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 删除用户
     *
     * @param userId user编号
     * @return User
     */
    @Override
    public boolean deleteUser(Integer userId) {
        User user = Optional.ofNullable(userMapper.selectById(userId))
                .orElseThrow(() -> new ServiceException("user不存在！"));
        ;
        return userMapper.updateById(user) > 0;
    }

    @Override
    public User userLogin(String account, String password) {
        LambdaQueryWrapper<User> query = Wrappers.lambdaQuery();
        query.eq(User::getAccount, account)
                .eq(User::getPwd, password)
                .eq(User::getStatus, 1);
        User user = Optional.ofNullable(userMapper.selectOne(query))
                .orElseThrow(() -> new ServiceException("请检查用户名和密码是否正确！"));
        User userDto=new User();
        BeanUtil.copyProperties (user, userDto);
        return userDto;
    }


    /**
     * 用户 条件构造器
     *
     * @param userReq 用户 查询条件
     * @return Wrappers
     */
    public LambdaQueryWrapper<User> getUserQuery(UserPageReq userReq) {
        LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(User::getStatus, 1);
        return lambdaQuery;
    }
}
